package com.EAsystem.controller.admin;

import com.EAsystem.annotation.RequireRole;
import com.EAsystem.entity.Announcement;
import com.EAsystem.service.AnnouncementService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin")
@RequireRole({1}) // 只有管理员可以访问
public class ClassroomController {

    @Autowired
    private AnnouncementService announcementService;

    @GetMapping("/notifications/classroom")
    public String listClassroomNotifications(@RequestParam(defaultValue = "1") int page,
                                          @RequestParam(defaultValue = "5") int size,
                                          Model model,
                                          @RequestParam(required = false) String titleSearch) {
        PageInfo<Announcement> pageInfo;
        if (titleSearch != null && !titleSearch.isEmpty()) {
            Announcement classroom = new Announcement();
            classroom.setTitle(titleSearch);
            classroom.setType(3);
            pageInfo = announcementService.findByConditionPage(classroom, page, size);
        } else {
            pageInfo = announcementService.findByTypeThreeWithPage(page, size);
        }
        if (pageInfo == null) pageInfo = new PageInfo<>();
        model.addAttribute("classrooms", pageInfo.getList());
        model.addAttribute("currentPage", page);
        model.addAttribute("pageSize", size);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("totalRecords", pageInfo.getTotal());
        model.addAttribute("titleSearch", titleSearch);
        return "admin/classroom_notifications";
    }

    @PostMapping("/notifications/classroom/add")
    public String addClassroom(Announcement classroom) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        classroom.setCreateTime(now);
        classroom.setUpdateTime(now);
        classroom.setStatus(1);
        classroom.setType(3);
        announcementService.save(classroom);
        return "redirect:/admin/notifications/classroom";
    }

    @GetMapping("/notifications/classroom/delete/{id}")
    public String deleteClassroom(@PathVariable Integer id) {
        announcementService.delete(id);
        return "redirect:/admin/notifications/classroom";
    }

    @GetMapping("/notifications/classroom/batchDelete")
    public String batchDelete(@RequestParam("ids") String ids) {
        for (String id : ids.split(",")) {
            announcementService.delete(Integer.valueOf(id));
        }
        return "redirect:/admin/notifications/classroom";
    }

    @GetMapping("/notifications/classroom/view/{id}")
    @ResponseBody
    public Announcement viewClassroom(@PathVariable Integer id) {
        Announcement c = announcementService.findById(id);
        if (c == null) throw new RuntimeException("未找到该教室安排");
        return c;
    }

    @PostMapping("/notifications/classroom/edit")
    public String editClassroom(Announcement classroom) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        classroom.setUpdateTime(now);
        classroom.setType(3);
        announcementService.edit(classroom.getId(), classroom.getTitle(), classroom.getContent(), classroom.getType(), classroom.getUpdateTime());
        return "redirect:/admin/notifications/classroom";
    }
} 